/*
 * 
 */
package diagramaclases.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.update.DiagramUpdater;

import diagramaclases.Atributo;
import diagramaclases.Clase;
import diagramaclases.DiagramaClases;
import diagramaclases.DiagramaclasesPackage;
import diagramaclases.Metodo;
import diagramaclases.Relacion;
import diagramaclases.diagram.edit.parts.AtributoEditPart;
import diagramaclases.diagram.edit.parts.ClaseClaseAtributosCompartmentEditPart;
import diagramaclases.diagram.edit.parts.ClaseClaseMetodosCompartmentEditPart;
import diagramaclases.diagram.edit.parts.ClaseEditPart;
import diagramaclases.diagram.edit.parts.DiagramaClasesEditPart;
import diagramaclases.diagram.edit.parts.MetodoEditPart;
import diagramaclases.diagram.edit.parts.RelacionEditPart;
import diagramaclases.diagram.providers.DiagramaclasesElementTypes;

/**
 * @generated
 */
public class DiagramaclasesDiagramUpdater {

	/**
	 * @generated
	 */
	public static boolean isShortcutOrphaned(View view) {
		return !view.isSetElement() || view.getElement() == null
				|| view.getElement().eIsProxy();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesNodeDescriptor> getSemanticChildren(
			View view) {
		switch (DiagramaclasesVisualIDRegistry.getVisualID(view)) {
		case DiagramaClasesEditPart.VISUAL_ID:
			return getDiagramaClases_1000SemanticChildren(view);
		case ClaseClaseAtributosCompartmentEditPart.VISUAL_ID:
			return getClaseClaseAtributosCompartment_7001SemanticChildren(view);
		case ClaseClaseMetodosCompartmentEditPart.VISUAL_ID:
			return getClaseClaseMetodosCompartment_7002SemanticChildren(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesNodeDescriptor> getDiagramaClases_1000SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		DiagramaClases modelElement = (DiagramaClases) view.getElement();
		LinkedList<DiagramaclasesNodeDescriptor> result = new LinkedList<DiagramaclasesNodeDescriptor>();
		for (Iterator<?> it = modelElement.getClases().iterator(); it.hasNext();) {
			Clase childElement = (Clase) it.next();
			int visualID = DiagramaclasesVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == ClaseEditPart.VISUAL_ID) {
				result.add(new DiagramaclasesNodeDescriptor(childElement,
						visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesNodeDescriptor> getClaseClaseAtributosCompartment_7001SemanticChildren(
			View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.emptyList();
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.emptyList();
		}
		Clase modelElement = (Clase) containerView.getElement();
		LinkedList<DiagramaclasesNodeDescriptor> result = new LinkedList<DiagramaclasesNodeDescriptor>();
		for (Iterator<?> it = modelElement.getAtributos().iterator(); it
				.hasNext();) {
			Atributo childElement = (Atributo) it.next();
			int visualID = DiagramaclasesVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == AtributoEditPart.VISUAL_ID) {
				result.add(new DiagramaclasesNodeDescriptor(childElement,
						visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesNodeDescriptor> getClaseClaseMetodosCompartment_7002SemanticChildren(
			View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.emptyList();
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.emptyList();
		}
		Clase modelElement = (Clase) containerView.getElement();
		LinkedList<DiagramaclasesNodeDescriptor> result = new LinkedList<DiagramaclasesNodeDescriptor>();
		for (Iterator<?> it = modelElement.getMetodos().iterator(); it
				.hasNext();) {
			Metodo childElement = (Metodo) it.next();
			int visualID = DiagramaclasesVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == MetodoEditPart.VISUAL_ID) {
				result.add(new DiagramaclasesNodeDescriptor(childElement,
						visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getContainedLinks(View view) {
		switch (DiagramaclasesVisualIDRegistry.getVisualID(view)) {
		case DiagramaClasesEditPart.VISUAL_ID:
			return getDiagramaClases_1000ContainedLinks(view);
		case ClaseEditPart.VISUAL_ID:
			return getClase_2001ContainedLinks(view);
		case AtributoEditPart.VISUAL_ID:
			return getAtributo_3001ContainedLinks(view);
		case MetodoEditPart.VISUAL_ID:
			return getMetodo_3002ContainedLinks(view);
		case RelacionEditPart.VISUAL_ID:
			return getRelacion_4001ContainedLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getIncomingLinks(View view) {
		switch (DiagramaclasesVisualIDRegistry.getVisualID(view)) {
		case ClaseEditPart.VISUAL_ID:
			return getClase_2001IncomingLinks(view);
		case AtributoEditPart.VISUAL_ID:
			return getAtributo_3001IncomingLinks(view);
		case MetodoEditPart.VISUAL_ID:
			return getMetodo_3002IncomingLinks(view);
		case RelacionEditPart.VISUAL_ID:
			return getRelacion_4001IncomingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getOutgoingLinks(View view) {
		switch (DiagramaclasesVisualIDRegistry.getVisualID(view)) {
		case ClaseEditPart.VISUAL_ID:
			return getClase_2001OutgoingLinks(view);
		case AtributoEditPart.VISUAL_ID:
			return getAtributo_3001OutgoingLinks(view);
		case MetodoEditPart.VISUAL_ID:
			return getMetodo_3002OutgoingLinks(view);
		case RelacionEditPart.VISUAL_ID:
			return getRelacion_4001OutgoingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getDiagramaClases_1000ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getClase_2001ContainedLinks(
			View view) {
		Clase modelElement = (Clase) view.getElement();
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Relacion_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getAtributo_3001ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getMetodo_3002ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getRelacion_4001ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getClase_2001IncomingLinks(
			View view) {
		Clase modelElement = (Clase) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_Relacion_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getAtributo_3001IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getMetodo_3002IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getRelacion_4001IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getClase_2001OutgoingLinks(
			View view) {
		Clase modelElement = (Clase) view.getElement();
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		result.addAll(getOutgoingTypeModelFacetLinks_Relacion_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getAtributo_3001OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getMetodo_3002OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<DiagramaclasesLinkDescriptor> getRelacion_4001OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	private static Collection<DiagramaclasesLinkDescriptor> getContainedTypeModelFacetLinks_Relacion_4001(
			Clase container) {
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		for (Iterator<?> links = container.getRelaciones().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Relacion) {
				continue;
			}
			Relacion link = (Relacion) linkObject;
			if (RelacionEditPart.VISUAL_ID != DiagramaclasesVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Clase dst = link.getDestino();
			Clase src = link.getOrigen();
			result.add(new DiagramaclasesLinkDescriptor(src, dst, link,
					DiagramaclasesElementTypes.Relacion_4001,
					RelacionEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<DiagramaclasesLinkDescriptor> getIncomingTypeModelFacetLinks_Relacion_4001(
			Clase target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != DiagramaclasesPackage.eINSTANCE
					.getRelacion_Destino()
					|| false == setting.getEObject() instanceof Relacion) {
				continue;
			}
			Relacion link = (Relacion) setting.getEObject();
			if (RelacionEditPart.VISUAL_ID != DiagramaclasesVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Clase src = link.getOrigen();
			result.add(new DiagramaclasesLinkDescriptor(src, target, link,
					DiagramaclasesElementTypes.Relacion_4001,
					RelacionEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<DiagramaclasesLinkDescriptor> getOutgoingTypeModelFacetLinks_Relacion_4001(
			Clase source) {
		Clase container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Clase) {
				container = (Clase) element;
			}
		}
		if (container == null) {
			return Collections.emptyList();
		}
		LinkedList<DiagramaclasesLinkDescriptor> result = new LinkedList<DiagramaclasesLinkDescriptor>();
		for (Iterator<?> links = container.getRelaciones().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Relacion) {
				continue;
			}
			Relacion link = (Relacion) linkObject;
			if (RelacionEditPart.VISUAL_ID != DiagramaclasesVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Clase dst = link.getDestino();
			Clase src = link.getOrigen();
			if (src != source) {
				continue;
			}
			result.add(new DiagramaclasesLinkDescriptor(src, dst, link,
					DiagramaclasesElementTypes.Relacion_4001,
					RelacionEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static final DiagramUpdater TYPED_INSTANCE = new DiagramUpdater() {
		/**
		 * @generated
		 */
		@Override
		public List<DiagramaclasesNodeDescriptor> getSemanticChildren(View view) {
			return DiagramaclasesDiagramUpdater.getSemanticChildren(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<DiagramaclasesLinkDescriptor> getContainedLinks(View view) {
			return DiagramaclasesDiagramUpdater.getContainedLinks(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<DiagramaclasesLinkDescriptor> getIncomingLinks(View view) {
			return DiagramaclasesDiagramUpdater.getIncomingLinks(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<DiagramaclasesLinkDescriptor> getOutgoingLinks(View view) {
			return DiagramaclasesDiagramUpdater.getOutgoingLinks(view);
		}
	};

}
